package com.web.oa.shiro;

import com.web.oa.pojo.ActiveUser;
import com.web.oa.pojo.Employee;
import com.web.oa.pojo.MenuTree;
import com.web.oa.pojo.SysPermission;
import com.web.oa.service.EmployeeService;
import com.web.oa.service.SysService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class CustomRealm extends AuthorizingRealm {
    @Autowired
    private SysService sysService;

    @Autowired
    private EmployeeService employeeService;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("进来执行了~~~~~~~~~~~~");
        String username = token.getPrincipal().toString();
        System.out.println("username:"+username);
        Employee user=null;
        List<MenuTree> menuTree=null;

        try {
            user=employeeService.findSysUserByUserCode(username);
            menuTree=sysService.loadMenuTree();
        }catch (Exception e){
            e.printStackTrace();
        }
        if(user==null){
            return null;
        }
        String salt = user.getSalt();
        String password = user.getPassword();
        System.out.println("password:"+password);
        System.out.println("salt:"+salt);



        ActiveUser activeUser=new ActiveUser();
        activeUser.setId(user.getId());
        activeUser.setUserid(user.getName());
        activeUser.setUsername(user.getName());
        activeUser.setMenuTree(menuTree);
        activeUser.setUsercode(user.getName());
        activeUser.setManagerId(user.getManagerId());

        SimpleAuthenticationInfo sai=new SimpleAuthenticationInfo(activeUser,password, ByteSource.Util.bytes(salt),"自定义的realm");
        System.out.println("sai:"+sai);
        return sai;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        ActiveUser activeUser = (ActiveUser) principal.getPrimaryPrincipal();

        List<SysPermission> permissions = null;

        try {
            permissions= this.sysService.findPermissionListByUserId(activeUser.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
        }
        SimpleAuthorizationInfo sai=new SimpleAuthorizationInfo();
        List<String>listPermissons=new ArrayList<>();

        for (SysPermission permission : permissions) {
            listPermissons.add(permission.getPercode());
        }
        sai.addStringPermissions(listPermissons);
        return sai;
    }


}
